Conditions | 24 |
Paths | 22 |
Total Lines | 97 |
Code Lines | 50 |
Lines | 0 |
Ratio | 0 % |
Tests | 40 |
CRAP Score | 24.0082 |
Changes | 2 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like floatify.js ➔ ... ➔ parse often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | 'use strict'; |
||
60 | 231 | var parse = function parse(str) { |
|
61 | 231 | var string = str; |
|
62 | var spacePos; |
||
63 | var spaceSplit; |
||
64 | var spaceCount; |
||
65 | var dotPos; |
||
66 | var commaPos; |
||
67 | var lDotPos; |
||
68 | var lCommaPos; |
||
69 | var dotCount; |
||
70 | var commaCount; |
||
71 | |||
72 | 231 | string = string.trim(); |
|
73 | |||
74 | // 1st dot position |
||
75 | 231 | dotPos = string.indexOf('.'); |
|
76 | // 1st comma position |
||
77 | 231 | commaPos = string.indexOf(','); |
|
78 | // 1st space position |
||
79 | 231 | spacePos = string.indexOf(' '); |
|
80 | |||
81 | 231 | if (dotPos + commaPos + spacePos === -3) { |
|
82 | // life is good, no separators |
||
83 | 18 | return toFloatFormat(string); |
|
84 | } |
||
85 | |||
86 | 213 | spaceSplit = string.split(' '); |
|
87 | 213 | spaceCount = spaceSplit.length - 1; |
|
88 | 213 | dotCount = string.split('.').length - 1; |
|
89 | 213 | commaCount = string.split(',').length - 1; |
|
90 | |||
91 | // only combination of 2 separators allowed |
||
92 | 213 | if (dotCount > 0 && commaCount > 0 && spaceCount > 0) { |
|
93 | 1 | return Number.NaN; |
|
94 | } |
||
95 | |||
96 | // if there is any separator (space, comma, dot) found more than once, |
||
97 | // all other must not be found more than once |
||
98 | 212 | if (dotCount > 1 && (commaCount > 1 || spaceCount > 1)) { |
|
99 | 4 | return Number.NaN; |
|
100 | } |
||
101 | 208 | if (commaCount > 1 && (dotCount > 1 || spaceCount > 1)) { |
|
102 | 1 | return Number.NaN; |
|
103 | } |
||
104 | 207 | if (spaceCount > 1 && (dotCount > 1 || commaCount > 1)) { |
|
105 | return Number.NaN; |
||
106 | } |
||
107 | |||
108 | 207 | if (spaceCount > 0) { |
|
109 | 72 | if (!string.match(/^(\d{1,3})?(\s\d{3})*([,\.]\d+)?$/)) { |
|
110 | 17 | return Number.NaN; |
|
111 | } |
||
112 | 55 | string = spaceSplit.join(''); |
|
113 | } |
||
114 | |||
115 | 190 | if (dotPos !== -1 && commaPos !== -1) { |
|
116 | // format is using dot and comma |
||
117 | |||
118 | // last dot position |
||
119 | 61 | lDotPos = string.lastIndexOf('.'); |
|
120 | // last comma position |
||
121 | 61 | lCommaPos = string.lastIndexOf(','); |
|
122 | |||
123 | // order of 1st dot -> comma must be same as last dot -> comma |
||
124 | // 123.123.123,123 -> ok 123.123,123.123 -> not ok |
||
125 | 61 | if (Math.sign(dotPos - commaPos) !== Math.sign(lDotPos - lCommaPos)) { |
|
126 | 3 | return Number.NaN; |
|
127 | } |
||
128 | |||
129 | // check positions to guess the thousands separator |
||
130 | 58 | if (dotPos > commaPos) { |
|
131 | 51 | if (dotCount > 1) { |
|
132 | 1 | return Number.NaN; |
|
133 | } |
||
134 | // best guess: . is thousands separator and , is decimal point |
||
135 | 50 | return toFloatFormat(string, ',', '.'); |
|
136 | } |
||
137 | |||
138 | 7 | if (commaCount > 1) { |
|
139 | 1 | return Number.NaN; |
|
140 | } |
||
141 | // best guess: , is thousands separator and . is decimal point |
||
142 | 6 | return toFloatFormat(string, '.', ','); |
|
143 | } |
||
144 | |||
145 | 129 | if (dotPos !== -1) { |
|
146 | // only dot(s) in format |
||
147 | 85 | return parseParts(string, '.', dotCount); |
|
148 | } |
||
149 | |||
150 | 44 | if (commaPos !== -1) { |
|
151 | // only comma(s) in format |
||
152 | 39 | return parseParts(string, ',', commaCount); |
|
153 | } |
||
154 | |||
155 | 5 | return toFloatFormat(string); |
|
156 | }; |
||
157 | |||
166 |